#include "JobsQueue.h"

JobsQueue::JobsQueue() :
	jobs()
{
	pthread_mutex_init(&lock, NULL);
	pthread_cond_init(&cond, NULL);
}

JobsQueue::~JobsQueue()
{
	pthread_cond_destroy(&cond);
	pthread_mutex_destroy(&lock);
}

void JobsQueue::addJob(Job *job)
{

	pthread_mutex_lock(&lock);

	jobs.push(job);
	pthread_cond_signal(&cond);

	pthread_mutex_unlock(&lock);

}

Job* JobsQueue::waitForJob()
{

	pthread_mutex_lock(&lock);

	while (jobs.empty())
		pthread_cond_wait(&cond, &lock);

	Job *res = jobs.front();
	jobs.pop();

	pthread_mutex_unlock(&lock);

	return res;
}

